package pl.edu.pjwstk.kor.sbql4j.project1;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;
import pl.edu.pjwstk.kor.model.Service;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.Struct;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project1Query_SbqlQuery5Db4o0 implements Db4oSBQLQuery {
    public Project1Query_SbqlQuery5Db4o0() {
    }

    /**
     * query='db.(Rank as r).( count(r.getCustomers()) group as ilosc, r as rank).(ilosc as ilosc, rank as rank)'
    '
     **/
    public java.util.Collection<pl.wcislo.sbql4j.java.model.runtime.Struct> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Rank> _ident_Rank = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Rank>();
        ClassMetadata _classMeta7 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Rank");
        long[] _ids7 = _classMeta7.getIDs(transLocal);

        for (long _id7 : _ids7) {
            LazyObjectReference _ref7 = transLocal.lazyReferenceFor((int) _id7);
            _ident_Rank.add((pl.edu.pjwstk.kor.model.Rank) _ref7.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Rank> _asResult_r = _ident_Rank;
        java.util.Collection<pl.wcislo.sbql4j.java.model.runtime.Struct> _dotResult1 =
            new java.util.ArrayList<pl.wcislo.sbql4j.java.model.runtime.Struct>();
        int _dotIndex1 = 0;

        for (pl.edu.pjwstk.kor.model.Rank _dotEl1 : _asResult_r) {
            if (_dotEl1 == null) {
                continue;
            }

            if (_dotEl1 != null) {
                ocb.activate(_dotEl1, 2);
            }

            pl.edu.pjwstk.kor.model.Rank _ident_r = _dotEl1;

            if (_ident_r != null) {
                ocb.activate(_ident_r, 1);
            }

            pl.edu.pjwstk.kor.model.Rank _dotEl = _ident_r;

            if (_ident_r != null) {
                ocb.activate(_ident_r, 2);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Customer> _mth_getCustomersResult =
                _dotEl.getCustomers();

            if (_mth_getCustomersResult != null) {
                ocb.activate(_mth_getCustomersResult, 2);
            }

            java.lang.Integer _countResult = _mth_getCustomersResult.size();
            java.lang.Integer _groupAsResultilosc = _countResult;
            pl.edu.pjwstk.kor.model.Rank _ident_r1 = _dotEl1;

            if (_ident_r1 != null) {
                ocb.activate(_ident_r1, 1);
            }

            pl.edu.pjwstk.kor.model.Rank _asResult_rank = _ident_r1;
            pl.wcislo.sbql4j.java.model.runtime.Struct _commaResult = OperatorUtils.cartesianProductSS(_groupAsResultilosc,
                    _asResult_rank, "ilosc", "rank");

            if (_commaResult != null) {
                ocb.activate(_commaResult, 2);
            }

            _dotResult1.add(_commaResult);
            _dotIndex1++;
        }

        java.util.Collection<pl.wcislo.sbql4j.java.model.runtime.Struct> _dotResult2 =
            new java.util.ArrayList<pl.wcislo.sbql4j.java.model.runtime.Struct>();
        int _dotIndex2 = 0;

        for (pl.wcislo.sbql4j.java.model.runtime.Struct _dotEl2 : _dotResult1) {
            if (_dotEl2 == null) {
                continue;
            }

            if (_dotEl2 != null) {
                ocb.activate(_dotEl2, 2);
            }

            java.lang.Integer _ident_ilosc = (java.lang.Integer) _dotEl2.get(
                    "ilosc");

            if (_ident_ilosc != null) {
                ocb.activate(_ident_ilosc, 1);
            }

            java.lang.Integer _asResult_ilosc = _ident_ilosc;
            pl.edu.pjwstk.kor.model.Rank _ident_rank = (pl.edu.pjwstk.kor.model.Rank) _dotEl2.get(
                    "rank");

            if (_ident_rank != null) {
                ocb.activate(_ident_rank, 1);
            }

            pl.edu.pjwstk.kor.model.Rank _asResult_rank1 = _ident_rank;
            pl.wcislo.sbql4j.java.model.runtime.Struct _commaResult1 = OperatorUtils.cartesianProductSS(_asResult_ilosc,
                    _asResult_rank1, "ilosc", "rank");

            if (_commaResult1 != null) {
                ocb.activate(_commaResult1, 2);
            }

            _dotResult2.add(_commaResult1);
            _dotIndex2++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_dotResult2, ocb);

        return _dotResult2;
    }
}
